<template>
  <el-form label-width="100px" style="width: 500px">
    <el-form-item label="头像">
<!--   上传组件开始
  action设置上传地址
  limit设置上传数量
  name设置上传参数名称-->
      <el-upload
          v-model:file-list="fileList"
          action="http://localhost:8080/v1/upload"
          limit="1"
          name="file"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
      >
        <el-icon><Plus /></el-icon>
      </el-upload>

      <el-dialog v-model="dialogVisible">
        <img w-full :src="dialogImageUrl" alt="Preview Image" />
      </el-dialog>
<!--   上传组件结束   -->
    </el-form-item>
    <el-form-item label="昵称">
      <el-input placeholder="请输入昵称" v-model="user.nickname"></el-input>
    </el-form-item>
    <el-form-item label="用户名">
      <el-input :value="user.username" disabled></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="save()">保存修改</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

const user = ref(localStorage.user?JSON.parse(localStorage.user):null);

const save = ()=>{



  let newUser = {nickname:user.value.nickname,id:user.value.id};
  if (fileList.value.length>0){
    //得到图片路径
    let url = fileList.value[0].response.data;
    newUser.imgUrl = url;

    user.value.imgUrl = url;
  }

  let data = qs.stringify(newUser);
  axios.post('http://localhost:8080/v1/users/update',data)
  .then((response)=>{
    if (response.data.code==2001){
      ElMessage.success("修改完成!");
      //更新LocalStorage里面的数据
      localStorage.user = JSON.stringify(user.value);
      location.reload();
    }
  })

}

const fileList = ref([]);
const dialogImageUrl = ref('')
const dialogVisible = ref(false)
const handleRemove = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles)
}
const handlePictureCardPreview = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url
  dialogVisible.value = true
}
</script>

<style scoped>

</style>